<?php
require_once('./Controller.php');
require_once('../models/Content.php');
require_once('../models/Comment.php');

class EditController extends Controller{

    public function index($id){
        $model = new Content();
        $info = $model->getOne($id);

        $model = new Comment();
        $comment = $model->getComment($id);

        $data = [
            'info' => $info,
            'comment' => $comment,
        ];

//        $this->dd($data);

        $url = '../views/edit.html';

        $this->view($url,$data);
    }

    public function create($arr)
    {
        $model = new Comment();
        $data = [
            'blog_id' => $arr['blog_id'],
            'comment' => $arr['comment'],
            'comment_id' => $_SESSION['userid'],
        ];
        $id = $model->setComment($data);

        if($id){
            $url = '../controllers/EditController.php?id='.$arr['blog_id'];
            $this->redirect($url);
        }else{
            $url = '../controllers/EditController.php?id='.$arr['blog_id'];
            $this->redirect($url);
        }
    }

}

$edit = new EditController();

if(isset($_GET['id']) && !empty($_GET['id'])){
    $edit->index($_GET['id']);
}else{
    $edit->redirect('../controllers/IndexController.php');
}

if(isset($_POST['comment']) && !empty($_POST['comment'])){
    $edit->create($_POST);
}

